<?xml version="1.0"?>
<!DOCTYPE xsl:stylesheet [
  <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#">
  <!ENTITY dc "http://purl.org/dc/elements/1.1/">
  <!ENTITY dcterms "http://purl.org/dc/terms/">
  <!ENTITY foaf "http://xmlns.com/foaf/0.1/">
  <!ENTITY a "http://xmlns.heustics.org/schema/myelin/0.1/">
  <!ENTITY m "http://xmlns.heustics.org/ontology/myelin/0.1/">
]>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:exslt="http://exslt.org/common"
  xmlns:func="http://exslt.org/functions"
  xmlns:f="http://xmlns.4suite.org/ext"
  xmlns:l="http://xmlns.heustics.org/schema/i18n/0.1/"
  xmlns:a="&a;"
  xmlns:o="&a;SparqlService#view"
  xmlns:e="&a;editor-common#"
  xmlns:srx="http://www.w3.org/2005/sparql-results#"
  xmlns:w="http://xmlns.heustics.org/xslt/ext/wiki/0.1/"
  extension-element-prefixes="func w f"
  exclude-result-prefixes="xsl exslt func f o e a l srx"
> 
  <xsl:param name="sparql-layout"/>
  
  <xsl:variable name="o:l10n" select="a:get-l10n('&a;SparqlService#view')"/>
  <xsl:variable name="o:params-l10n" select="$o:l10n/*[@name='params']/*"/>
  <xsl:variable name="o:hints-l10n" select="$o:l10n/*[@name='hints']/*"/>
  <xsl:variable name="o:boolean-l10n" select="$o:l10n/*[@name='boolean-values']/*"/>
  <xsl:variable name="o:value-types-l10n" select="$o:l10n/*[@name='value-types']/*"/>
  
  <xsl:variable name="o:layout" select="o:sanitize-layout()"/>
  
  <func:function name="o:sanitize-layout">
    <xsl:param name="value" select="$sparql-layout"/>
    <xsl:choose>
      <xsl:when test="$value = 'col' or $value = 'row'">
        <func:result select="$value"/>
      </xsl:when>
      <xsl:otherwise>
        <func:result select="'row'"/>
      </xsl:otherwise>
    </xsl:choose>
  </func:function>  
    
  <!-- xsl:template match="a:search-params" mode="a:search-url-str">
    <xsl:value-of select="a:format-repeat-request()"/>
    <xsl:apply-templates mode="a:search-url-param-str"/>
    <xsl:choose>
      <xsl:when test="$o:layout = 'col' or $o:layout = 'row'">
        <xsl:value-of select="concat('&amp;sparql-layout=',$o:layout)"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>col</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template -->
  
  <xsl:template match="a:graph-result-method" mode="a:input-show">
    <xsl:param name="methods" select="$o:l10n/*[@name='graph-result-method']/*"/>
    <xsl:choose>
      <xsl:when test="count($methods)=1">
        <input type='hidden' name='method' value="{$methods}"/>
      </xsl:when>
      <xsl:otherwise>
        <div class='input options methods'>  
          <div class="key">
            <div>
              <span><xsl:value-of select="$o:params-l10n[@key='graph-result-method']"/></span>
              <div class="spacer"/>
            </div>        
            <div class='hint'>
              <xsl:call-template name="a:input-hints">
                <xsl:with-param name="hint" select="$o:hints-l10n[@key='graph-result-method']"/>
              </xsl:call-template>
              <div class="spacer"/>
            </div>
            <div class="spacer"/>
          </div>      
          <div class='value'>
            <xsl:variable name="selected" select="string(.)"/>
            <div class="entry">
              <select name="graph-result-method">
                <xsl:for-each select="$methods">
                  <option value='{@key}'>
                    <xsl:if test='@key = $selected'>
                      <xsl:attribute name='selected'>selected</xsl:attribute>
                    </xsl:if>      
                    <xsl:value-of select='.'/>
                  </option>
                </xsl:for-each>                
              </select>
              <div class="spacer"/>
            </div>        
            <div class="spacer"/>
          </div>
          <xsl:variable name="help-markup" select="$o:hints-l10n[@key='graph-result-method-help-markup']"/>
          <xsl:if test="$help-markup">
            <div class="entry help graph-result-method-help">
              <w:render-markup select='string($help-markup)' 
                mime-type='{$help-markup/@format}'
                url-params-string='{$a:common-url-params}' 
                url-resource-ext='{$a:resource-ext}'/>
              <div class="spacer"/>
            </div>
          </xsl:if>  
          <div class='spacer'/>
        </div>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template match="a:default-graph-uri|a:named-graph-uri" mode="a:input-show"> 
    <xsl:param name="key" select="local-name()"/>
    <xsl:param name="help-markup" select="$o:hints-l10n[@key=concat($key,'-help-markup')]"/>
    <div class='input textarea graph-uri {$key}'>      
      <div class='key'>
        <div>
          <xsl:call-template name="a:label-class-attr"/>
          <span><xsl:value-of select="$o:params-l10n[@key=$key]"/></span>
          <div class="spacer"/>
        </div>
        <div class='hint'>
          <xsl:call-template name="a:input-hints">
            <xsl:with-param name="hint" select="$o:hints-l10n[@key=$key]"/>
          </xsl:call-template>
          <div class="spacer"/>
        </div>
        <div class="spacer"/>
      </div>      
      <div class='value'>
        <div class="entry">
          <textarea class="max" name="{$key}" cols="50" rows="25">
            <xsl:value-of select="."/>
          </textarea>
          <div class="spacer"/>
        </div>     
        <xsl:if test="$help-markup">
          <div class="entry help {$key}-help">
            <w:render-markup select='string($help-markup)' 
              mime-type='{$help-markup/@format}'
              url-params-string='{$a:common-url-params}' 
              url-resource-ext='{$a:resource-ext}'/>
            <div class="spacer"/>
          </div>  
        </xsl:if> 
        <div class="spacer"/>
      </div>
      <div class='spacer'/>
    </div>
  </xsl:template> 
  
  <xsl:template match="a:query" mode="a:input-show">
    <input type='hidden' name='sparql-layout' value="{$o:layout}"/>
    <xsl:call-template name="a:query-input-show">
      <xsl:with-param name="help-markup" select="$o:l10n/*[@name='query-help-markup']"/>
    </xsl:call-template>
  </xsl:template>
    
  <xsl:template match="srx:sparql" mode="a:search-results">
    <xsl:param name="url"/>
    <div class="sparql">
      <xsl:apply-templates mode="srx:sparql">
        <xsl:with-param name="vars" select="srx:head/srx:variable"/>
        <xsl:with-param name="url" select="$url"/>
      </xsl:apply-templates>
      <div class="spacer sparql-results-spacer"/>
    </div>
  </xsl:template>
  
  <xsl:template match="srx:head" mode="srx:sparql"/>
  
  <!-- Don't need right now
    xsl:template match="srx:head[srx:link]" mode="srx:sparql">
    <div class="sparql-section sparql-head">
      <h2>Header</h2>
      <div class="sparql-head-links">
        <xsl:apply-templates mode="srx:head"/>
        <div class="spacer sparql-head-body-spacer"/>
      </div>
      <div class="spacer sparql-head-spacer"/>  
    </div>
  </xsl:template>
  
  <xsl:template match="srx:link" mode="srx:head">
    <div class="sparql-head-link">
      Additional query metadata file is <a href="{@href}">available</a>.
      <div class="spacer"/>
    </div>
  </xsl:template>
  
  <xsl:template match="srx:variable" mode="srx:head"/>
  
  <xsl:template match="srx:variable" mode="srx:head">
    <div class="sparql-variable">
      <span><xsl:value-of select="@name"/></span>
      <div class="spacer sparql-variable-spacer"/>
    </div>
  </xsl:template -->
  
  <xsl:template match="srx:boolean" mode="srx:sparql">
    <xsl:param name="l10n" select="$o:l10n/*[@name='ask-query']/*"/>
    <div class="sparql-section sparql-boolean-results">
      <h3>
        <xsl:value-of select="$l10n[@key='title']"/>
      </h3>
      <div class="sparql-results-body">
        <xsl:variable name="value" select="string(.)"/>
        <span>
          <xsl:value-of select="$o:boolean-l10n[@key=$value]"/>
        </span>
        <div class="spacer sparql-results-body-spacer"/>
      </div>
    </div>
  </xsl:template>
  
  <xsl:template match="srx:results" mode="srx:sparql">
    <xsl:param name="vars" select="../srx:head/srx:variable"/>
    <xsl:param name="url"/>
    <xsl:param name="l10n" select="$o:l10n/*[@name='select-query']/*"/>
    <xsl:variable name="results-count" select="count(*)"/>
    <div class="sparql-section sparql-results sparql-binding-results sparql-header">
      <h3>
        <xsl:value-of select="$l10n[@key='title']"/>      
      </h3>
      <xsl:if test="$results-count">
        <div class="sparql-layout-nav">
          <div class="header">
            <span>
              <xsl:value-of select="$l10n[@key='layout-select-title']"/>
            </span>
            <div class="spacer"/>
          </div>
          <div class="body">
            <div class="row-col-select">
              <ul>
                <li class="first col">
                  <xsl:choose>
                    <xsl:when test="$o:layout = 'col'">
                      <span>
                        <xsl:value-of select="$l10n[@key='layout-col']"/>
                      </span>
                      <input type="hidden" name="sparql-layout" value="col"/>
                    </xsl:when>
                    <xsl:otherwise>
                      <a href="{concat($url,'&amp;sparql-layout=col')}">
                        <xsl:value-of select="$l10n[@key='layout-col']"/>
                      </a>
                    </xsl:otherwise>
                  </xsl:choose>
                </li>
                <li class="last row">
                  <xsl:choose>
                    <xsl:when test="$o:layout = 'row'">
                      <span>
                        <xsl:value-of select="$l10n[@key='layout-row']"/>
                      </span>
                      <input type="hidden" name="sparql-layout" value="row"/>
                    </xsl:when>
                    <xsl:otherwise>
                      <a href="{concat($url,'&amp;sparql-layout=row')}">
                        <xsl:value-of select="$l10n[@key='layout-row']"/>
                      </a>
                    </xsl:otherwise>
                  </xsl:choose>
                </li>
              </ul>
              <div class="spacer row-col-spacer"/>
            </div>            
            <div class="results-legend-wrapper">
              <div class="results-legend">
                <xsl:variable name="results-legend" select="$o:l10n/*[@name='results-legend']"/>
                <w:render-markup select='string($results-legend)' 
                  mime-type='{$results-legend/@format}'
                  url-params-string='{$a:common-url-params}' 
                  url-resource-ext='{$a:resource-ext}'/>
              </div>
              <div class="spacer results-legend-spacer"/>
            </div>
            <div class="spacer body-spacer"/>
          </div>
          <div class="spacer sparql-layout-spacer"/>
        </div>
      </xsl:if>
      <div class="spacer sparql-section-spacer sparql-header-spacer"/>
    </div>
    <!-- div class="sparql-results-attributes">
      <div class="sparql-results-attribute sparql-results-ordered">
        <div class="key">
          <span>Ordered</span>
          <div class="spacer"/>
        </div>
        <div class="value">
          <span><xsl:value-of select="@ordered"/></span>
          <div class="spacer"/>
        </div>
        <div class="spacer sparql-results-attribute-spacer"/>
      </div>
      <div class="sparql-results-attribute sparql-results-distinct">
        <div class="key">
          <span>Distinct</span>
          <div class="spacer"/>
        </div>
        <div class="value">
          <span><xsl:value-of select="@distinct"/></span>
          <div class="spacer"/>
        </div>
        <div class="spacer sparql-results-attribute-spacer"/>
      </div>
      <div class="spacer sparql-section-spacer sparql-results-attributes-spacer"/>
    </div -->
    <div class="sparql-section sparql-results-body">
      <xsl:choose>
        <xsl:when test="$results-count = 0">
          <div class="sparql-no-results">
            <span>
              <xsl:value-of select="$l10n[@key='no-results']"/>
            </span>
            <div class="spacer sparql-no-results-spacer"/>
          </div>
        </xsl:when>
        <xsl:when test="$o:layout = 'col'">
          <div class="sparql-layout-col">
            <table>
              <tr class="sparql-results-header">
                <xsl:apply-templates select="$vars" mode="srx:results-header-col">
                </xsl:apply-templates>
              </tr>
              <xsl:apply-templates mode="srx:results-col">
                <xsl:with-param name="vars" select="$vars"/>
              </xsl:apply-templates>
            </table>
          </div>
        </xsl:when>
        <xsl:otherwise>
          <div class="sparql-layout-row">
            <table>
              <xsl:apply-templates mode="srx:results-row">
                <xsl:with-param name="vars" select="$vars"/>
              </xsl:apply-templates>
            </table>
          </div>
        </xsl:otherwise>
      </xsl:choose>
      <div class="spacer sparql-section-spacer"/>
    </div>
  </xsl:template>
  
  <xsl:template match="srx:variable" mode="srx:results-header-col">
    <xsl:param name="width"/>
    <th>
      <span><xsl:value-of select="@name"/></span>
      <!-- div class="spacer sparql-results-col-spacer"/ -->
    </th>
  </xsl:template>
  
  <xsl:template match="srx:result" mode="srx:results-col">
    <xsl:param name="vars" select="../../srx:head/srx:variable"/>
    <xsl:variable name="bindings" select="*"/>
    <tr>
      <xsl:for-each select="$vars/@name">
        <xsl:variable name="name" select="string(.)"/>
        <td>
          <xsl:variable name="bind" select="$bindings[@name=$name]"/>
          <xsl:choose>
            <xsl:when test="$bind">
              <xsl:apply-templates select="$bindings[@name=$name]/*" mode="srx:results-binding"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:call-template name="srx:unbound-variable-result"/>
            </xsl:otherwise>
          </xsl:choose>
        </td>
      </xsl:for-each>
    </tr>
  </xsl:template>
  
  <xsl:template match="srx:result" mode="srx:results-row">
    <xsl:param name="vars" select="../../srx:head/srx:variable"/>
    <xsl:variable name="bindings" select="*"/>
    <xsl:choose>
      <xsl:when test="count($vars)=1">
        <xsl:variable name="name" select="$vars/@name"/>
        <tr class="first-last">
          <th>
            <span>
              <xsl:value-of select="$name"/>
            </span>
          </th>
          <td>
            <xsl:variable name="bind" select="$bindings[@name=$name]"/>
            <xsl:choose>
              <xsl:when test="$bind">
                <xsl:apply-templates select="$bindings[@name=$name]/*" mode="srx:results-binding"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:call-template name="srx:unbound-variable-result"/>
              </xsl:otherwise>
            </xsl:choose>
          </td>
        </tr>
        <tr class="sparql-result-sep"/>
      </xsl:when>
      <xsl:when test="count($vars) &gt; 1">
        <xsl:for-each select="$vars/@name">
          <xsl:variable name="name" select="string(.)"/>
          <tr>
            <xsl:choose>
              <xsl:when test="position()=last()">
                <xsl:attribute name="class">last</xsl:attribute>
              </xsl:when>
              <xsl:when test="position()=1">
                <xsl:attribute name="class">first</xsl:attribute>
              </xsl:when>
            </xsl:choose>
            <th>
              <span>
                <xsl:value-of select="$name"/>
              </span>
            </th>
            <td>
              <xsl:variable name="bind" select="$bindings[@name=$name]"/>
              <xsl:choose>
                <xsl:when test="$bind">
                  <xsl:apply-templates select="$bindings[@name=$name]/*" mode="srx:results-binding"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:call-template name="srx:unbound-variable-result"/>
                </xsl:otherwise>
              </xsl:choose>
            </td>
          </tr>
        </xsl:for-each>
        <tr class="sparql-result-sep"/>
      </xsl:when>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template name="srx:unbound-variable-result">    
    <xsl:param name="l10n" select="$o:value-types-l10n"/>
    <div class="sparql-binding-value sparql-value-unbound">
      <span>
        <xsl:value-of select="$l10n[@key='unbound-variable']"/>
      </span>
      <div class="spacer sparql-binding-variable-spacer"/>
    </div>
  </xsl:template>
  
  <xsl:template match="srx:bnode" mode="srx:results-binding">
    <xsl:param name="l10n" select="$o:value-types-l10n"/>
    <div class="sparql-binding-value sparql-value-bnode">
      <span>
        <xsl:value-of select="concat($l10n[@key='bnode-marker-left'],.,$l10n[@key='bnode-marker-right'])"/>
      </span>
      <div class="spacer sparql-binding-variable-spacer"/>
    </div>
  </xsl:template>
  
  <xsl:template match="srx:uri" mode="srx:results-binding">
    <xsl:param name="l10n" select="$o:value-types-l10n"/>
    <div class="sparql-binding-value sparql-value-uri">
      <xsl:variable name="uri" select="string(.)"/>
      <xsl:choose>
        <xsl:when test="starts-with($uri,'urn:')">
          <span>
            <xsl:value-of select="$uri"/>
          </span>
        </xsl:when>
        <xsl:otherwise>
           <a href="{$uri}">
            <xsl:value-of select="concat(
              $l10n[@key='uri-marker-left'],
              $uri,
              $l10n[@key='uri-marker-right'])"/>
           </a>
        </xsl:otherwise>
      </xsl:choose>     
      <div class="spacer sparql-binding-variable-spacer"/>
    </div>
  </xsl:template>
  
  <xsl:template match="srx:literal" mode="srx:results-binding">
    <xsl:param name="l10n" select="$o:value-types-l10n"/>
    <div class="sparql-binding-value sparql-value-literal">
      <span>
        <xsl:value-of select="
          concat(
            $l10n[@key='literal-quote-left'],
            .,
            $l10n[@key='literal-quote-right']
          )"/>
        <xsl:choose>
          <xsl:when test="@datatype">
            <xsl:value-of select="concat($l10n[@key='datatype-marker'],'&lt;',@datatype,'&gt;')"/>
          </xsl:when>
          <xsl:when test="@xml:lang">
            <xsl:value-of select="concat($l10n[@key='lang-marker'],@xml:lang)"/>
          </xsl:when>
        </xsl:choose>
      </span>
      <div class="spacer sparql-binding-variable-spacer"/>
    </div>
  </xsl:template>
  
  <xsl:template match="srx:graph" mode="a:search-results">
    <xsl:param name="url"/>
    <xsl:param name="l10n" select="$o:l10n/*[@name='graph-result']/*"/>
    <div class="sparql sparql-graph">
      <div class="sparql-section sparql-header">
        <h3>
          <xsl:value-of select="$l10n[@key='title']"/>      
        </h3>
        <div class="spacer sparql-section-spacer sparql-header-spacer"/>
      </div>
      <div class="sparql-section sparql-graph-body">
        <xsl:variable name="result-rtf">
          <xsl:choose>
            <xsl:when test="@xml">
              <xsl:value-of select='f:xml-to-string(*,1)'/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select='.'/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>   
        <xsl:variable name="result" select="string($result-rtf)"/>   
        <xsl:choose>
          <xsl:when test="$result">
            <pre>
              <xsl:value-of
                select="$result"/>
            </pre>
          </xsl:when>
          <xsl:otherwise>
            <span>
              <xsl:value-of select="$l10n[@key='empty-graph']"/>
            </span>
          </xsl:otherwise>
        </xsl:choose>
        <div class="spacer sparql-section-spacer sparql-graph-body-spacer"/>
      </div>
      <div class="spacer sparql-spacer sparq-graph-spacer"/>
    </div>   
  </xsl:template>
    
</xsl:stylesheet>

